
import { AssetsMgr, g_bundleName } from "../../../general/Script/mgr/assetsManager";
import GameMgr from "../../../general/Script/mgr/gameManager";
import NetMgr from "../../../general/Script/mgr/netManager";
import SoundMgr from "../../../general/Script/mgr/soundManager";
import { SubGameData, subGameMgr } from "../../../general/Script/mgr/SubGameMgr";
import RankingList from "../../../general/Script/prefab/rankingList";
import { eHotUpdateEvent } from "../../../general/Script/update/HotUpdateEvent";
import { VersionTool } from "../../../general/Script/update/VersionTool";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SubGameCtrl extends cc.Component {
    @property(cc.Label)
    nameLabel: cc.Label = null;

    @property(cc.Label)
    rateLabel: cc.Label = null;

    @property(cc.Label)
    versionLabel: cc.Label = null;

    @property(cc.Node)
    newLayer: cc.Node = null;

    // @property(cc.SpriteFrame)
    // blockSprite: cc.SpriteFrame = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.newLayer.active = false;
    }

    start() {
        // this.node.getChildByName('default_sprite_splash').getComponent(cc.Sprite).spriteFrame = this.blockSprite;
    }

    eventListeners() {
        cc.systemEvent.targetOff(this);
        cc.systemEvent.on(eHotUpdateEvent.subGameHotUpdateRate + this.data.name, this.onSubGameHotUpdateRate, this);
        cc.systemEvent.on(eHotUpdateEvent.subGameHotUpdateFinish + this.data.name, this.onSubGameHotUpdateFinish, this);
        cc.systemEvent.on(eHotUpdateEvent.subGameCheckVersionResult + this.data.name, this.onSubGameCheckVersionResult, this);
    }

    protected onDisable(): void {
        cc.systemEvent.targetOff(this);
    }

    onSubGameHotUpdateRate(rate: number) {
        this.rateLabel.string = (rate * 100).toFixed(0) + "%";
    }

    onSubGameHotUpdateFinish() {
        this.rateLabel.string = "";
        this.newLayer.active = false;
        VersionTool.getSubGameVersion(this.data.name).then((version) => {
            this.versionLabel.string = "ver: " + version;
        });
    }

    /**
     * 热更新新版本检查结果
     *
     * @param newVersionFound 是否发现新版本
     */
    onSubGameCheckVersionResult(newVersionFound: boolean) {
        console.log("[SubGameCtrl]" + newVersionFound);
        this.newLayer.active = newVersionFound;
    }

    /**
     * 子游戏信息
     */
    data: SubGameData = null;

    init(data: SubGameData) {
        console.log('gameData = ' + JSON.stringify(data));
        this.data = data;
        this.nameLabel.string = data.gameName || data.name;
        this.eventListeners();
        VersionTool.getSubGameVersion(data.name).then((version) => {
            this.versionLabel.string = "ver: " + version;
            subGameMgr.checkUpdateSubGame(data.name, data.packageUrl + `${version}/`);
        });

    }

    onClick() {
        SoundMgr.Ins().playEffect('btn_touch', g_bundleName.resources);
        if (this.newLayer.active) {
            subGameMgr.hotUpdateSubGame(this.data.name);
            this.newLayer.active = false;
            return;
        }

        console.log('launchSubGame this.data.name = ', this.data.sceneName);
        subGameMgr.launchSubGame(this.data.name, this.data.sceneName);
    }


    async rankCallBack() {


        const rank = await NetMgr.Ins().getRankList(this.data.rankType);
        const prefab = await AssetsMgr.obj().loadRes(g_bundleName.resources, 'Prefab/rankingList') as cc.Prefab;

        const parent = cc.find('Canvas');
        const node = GameMgr.Ins().addOnlyOneNode(parent, cc.instantiate(prefab));
        node.getComponent(RankingList).init(rank.data, rank.user);

    }
    // update (dt) {}
}
